<template>
  <div>
    <input type="text" v-model="username" />

    <input type="text" v-model="password" @input="search" />
    <button @click="login">登录</button>
  </div>
</template>
<script>
export default {
  data () {
    return {
      username: 'admin',
      password: '123456'
    }
  },
  methods: {
    login () {
      localStorage.setItem('token', 'jack')
      this.$router.push({ name: 'Home' })
    },
    handleInput () {
      console.log('正在输入。。。。。。')
    },
    debounce (fn, time) {
      let timer = null
      // e是keyup执行的事件对象，将这个事件对象传递给fn
      return function (e) {
        clearTimeout(timer)
        timer = setTimeout(() => {
          fn.call(this, e)
        }, time)
      }
    }
  },
  created () {
    this.search = this.debounce(this.handleInput, 300)
  }
}
</script>
<style>
</style>
